#include <curl/curl.h>
#include <tinyxml.h>

#include <boost/filesystem/path.hpp>
#include <boost/filesystem/operations.hpp>

#include "AppWeatherModel.h"
#include "Weather.h"

#define URL "http://www.google.com/ig/api?weather="
#define TMP_FILE_NAME "tmp/Weather.xml"
#define ICON_LOCAL_PATH "app/weather_data"
#define ICON_REMOTE_PATH "http://www.google.com"

FILE* temp_weather;

size_t write_data_weather(void * ptr, size_t size, size_t nmeb, void *stream){
  return fwrite(ptr,size,nmeb,temp_weather);
}

AppWeatherModel::AppWeatherModel(){
  // Weather* w = new Weather("Grenoble","Wed","Sunny",50,60);
}

void AppWeatherModel::loadTownForecast(std::string town){
  _location = new std::string(town);
  temp_weather = fopen(TMP_FILE_NAME,"w+");

  std::string final_url(URL+town);

  /* process the file download form HTTP */
  {
    CURL* curl;
    curl = curl_easy_init();
    curl_easy_setopt(curl,CURLOPT_URL,final_url.c_str());
    curl_easy_setopt(curl,CURLOPT_WRITEFUNCTION,write_data_weather);
    curl_easy_perform(curl);
    curl_easy_cleanup(curl);
  }
  fclose(temp_weather);
  temp_weather = NULL;

  /* parse the XML downloaded file */
  {
    TiXmlDocument doc(TMP_FILE_NAME);
    doc.LoadFile();

    TiXmlHandle hRoot(&doc);
    {
      TiXmlElement* pResultsNode =
	hRoot.FirstChild("xml_api_reply").FirstChild("weather")
	.FirstChild("forecast_conditions").Element();
 
      for( ; 
	   pResultsNode; 
	   pResultsNode = 
	     pResultsNode->NextSiblingElement("forecast_conditions")) {
	//if(pResultsNode->ValueStr() == "forecast_conditions") {
	  TiXmlElement* el;
	  std::string* condition;
	  std::string* temp_l_str;
	  int temp_l;
	  std::string* temp_h_str;
	  int temp_h;
	  std::string* day;
	  std::string* icon;
	  std::string* local_icon;
	  
	  el = pResultsNode->FirstChildElement("condition");
	  condition = new std::string(el->Attribute("data"));

	  el = pResultsNode->FirstChildElement("low");
	  temp_l_str = new std::string(el->Attribute("data"));
	  temp_l = atoi(temp_l_str->c_str());
	  	  
	  el = pResultsNode->FirstChildElement("high");
	  temp_h_str = new std::string(el->Attribute("data"));
	  temp_h = atoi(temp_h_str->c_str());

	  el = pResultsNode->FirstChildElement("day_of_week");
	  day = new std::string(el->Attribute("data"));

	  el = pResultsNode->FirstChildElement("icon");
	  icon = new std::string(el->Attribute("data"));
	  
	  local_icon = new std::string(ICON_LOCAL_PATH + *icon);
	  // download icon if don't exists
	  if(!boost::filesystem::exists(*local_icon)){
	    temp_weather = fopen(local_icon->c_str(),"w+");
    
	    std::string* url = new std::string(ICON_REMOTE_PATH+*icon);
    
	     {
	       CURL* curl;
	       curl = curl_easy_init();
	       curl_easy_setopt(curl,CURLOPT_URL,url->c_str());
	       curl_easy_setopt(curl,CURLOPT_WRITEFUNCTION,
				write_data_weather);
	       curl_easy_perform(curl);
	       curl_easy_cleanup(curl);
	    }
	    fclose(temp_weather);
	    delete url;
	  }
	  
	  Weather *w = new Weather(town,*day,*condition,*local_icon,
				   temp_l,temp_h);
	  _forecast.push_back(w);
	  
	  delete condition;
	  delete temp_l_str;
	  delete temp_h_str;
	  delete day;
	  //delete w;
	  // }
	
      }
    }
  }  
	

}

std::list<Weather*> AppWeatherModel::getForecast(){
  return _forecast;
}

std::string AppWeatherModel::getLocation(){
  return *_location;
}

AppWeatherModel::~AppWeatherModel() {
  delete _location;
}

